// pages/cookies/cookies.js

var utils = require('../../utils/util');

Page({

  /**
   * 页面的初始数据
   */
  data: {

    renderHistoryList: {},

    getList: []

  },

  getStorageCookies() {
    let storageList = wx.getStorageSync('Cookies'),
      // 今天0点的时间戳 
      todayTime = new Date(new Date().toLocaleDateString()).getTime(),
      // 昨天0点的时间戳
      yesterdayTime = new Date(new Date().toLocaleDateString()).getTime() - (24 * 60 * 60 * 1000),

      renderHistoryList = this.data.renderHistoryList,

      getList = this.data.getList

    console.log(getList);

    renderHistoryList['todayHistory'] = []

    renderHistoryList['yesterdayHistory'] = []

    getList && getList.forEach(v => {

      if (v.historyTime > todayTime) {

        let theTime = this.computedTime(v.historyTime),

          exactTime = this.getTime(v.historyTime)

        v.theTime = theTime

        v.exactTime = exactTime

        renderHistoryList['todayHistory'].push(v)

      } else if (todayTime > v.historyTime && v.historyTime > yesterdayTime) {

        let theTime = this.computedTime(v.historyTime),

          exactTime = this.getTime(v.historyTime)

        v.theTime = theTime

        v.exactTime = exactTime

        renderHistoryList['yesterdayHistory'].push(v)

      } else {

        let theTime = this.computedTime(v.historyTime),

          exactTime = this.getTime(v.historyTime)

        v.theTime = theTime

        v.exactTime = exactTime

        let exactTimeArr = renderHistoryList[exactTime] || []

        exactTimeArr.push(v)

        renderHistoryList[exactTime] = exactTimeArr

      }

    })

    this.setData({

      renderHistoryList

    })

  },

  computedTime(time) {

    let theTime = ''

    if (new Date(time).getHours() > 12) {

      theTime = '下午' + (new Date(time).getHours() - 12) + ':' + new Date(time).getMinutes()

    } else if (new Date(time).getHours() == 12) {

      theTime = '下午' + new Date(time).getHours() + ':' + new Date(time).getMinutes()

    } else {

      theTime = '上午' + new Date(time).getHours() + ':' + new Date(time).

      getMinutes()

    }

    return theTime

  },

  getHistoryData() {

    let storageList = wx.getStorageSync('Cookies')

    let idArr = storageList.map(v => {
      return v.id
    })

    wx.request({

      url: 'http://localhost:2345/msbd/my-collect',

      data: {
        id: idArr.join(",")
      },

      success: res => {

        let getList = res.data.data.data

        getList && getList.forEach((v, i) => {

          storageList.forEach((a, b) => {

            if (v.id == a.id) {

              getList[i].historyTime = a.time

            }

          })

        })

        this.setData({

          getList

        })

        this.getStorageCookies()

      }

    })

  },

  getTime(time) {

    let date = new Date(time),

      exactTime = date.getFullYear() + '年' + date.getMonth() + '月' + date.getDate() + '日 星期' + utils.dealWeek(date.getDay())

    return exactTime

  },

  deleteHistory(e) {

    let id = e.currentTarget.dataset.del[0].id,


      storageIdArr = wx.getStorageSync('Cookies'),

      renderHistoryList = this.data.renderHistoryList,

      getList = this.data.getList

    // 删除本地存储的id
    storageIdArr.forEach((v, i) => {

      if (v.id == id) {

        storageIdArr.splice(i, 1)

      }

    })

    wx.setStorageSync('Cookies', storageIdArr)

    // 删除请求的数据（未分组）
    getList.forEach((v, i) => {

      if (v.id == id) {

        getList.splice(i, 1)

      }

    })

    // 删除已分日期的数据
    let deleteList = renderHistoryList[e.currentTarget.dataset.idx]

    deleteList.forEach((v, i) => {

      if (v.id == id) {

        deleteList.splice(i, i)

      }

    })

    renderHistoryList[e.currentTarget.dataset.idx] = deleteList

    this.setData({

      renderHistoryList

    })


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    this.getHistoryData()

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})